Page({
	data: {
		ssid: '',
		password: '',
		udp: '',
		port: 0,
		showClearBtn: false,
		isFirst: true,
		product_type: 'https://gitee.com/tpyszc/tpyszc_image/raw/master/wf.png', //产品类型
	},

	onLoad(opt) {
		let that = this
		const prt = wx.getStorageSync('product_type');

		if (prt === 'a11uQJCzUPe') {
			// 西纳舒岩盐气溶胶治疗仪
			this.setData({
				product_type: 'https://gitee.com/tpyszc/tpyszc_image/raw/master/xns.png'
			});
		}
		if (prt === 'a1L4GogBzON') {
			this.setData({
				product_type: 'https://gitee.com/tpyszc/tpyszc_image/raw/master/salt.png'
			});
		}

		wx.startWifi({
			success(res) {
				that.getWifiInfo();
			},
			fail: function (res) {
				wx.showToast({
					title: '请连接路由器!',
					duration: 2000,
					icon: 'none'
				})
			}
		})
		this.getWifiInfo()
	},
	getWifiInfo() {
		let that = this
		wx.getConnectedWifi({
			success(res) {
				if ('getConnectedWifi:ok' === res.errMsg) {
					that.setData({
						ssid: res.wifi.SSID,
						bssid: res.wifi.BSSID
					})
				} else {
					wx.showToast({
						title: '请连接路由器',
						duration: 2000,
						icon: 'none'
					})
				}
			},
			fail(res) {
				wx.showToast({
					title: '请连接路由器',
					duration: 2000,
					icon: 'none'
				})
			}
		})
	},
	onInputSSID(evt) {
		const {
			value
		} = evt.detail;
		this.setData({
			ssid: value,
		});
	},
	onInputPassword(evt) {
		const {
			value
		} = evt.detail;
		this.setData({
			password: value,
			showClearBtn: !!value.length,
			isWaring: false,
		});
	},
	onClear() {
		this.setData({
			password: '',
			showClearBtn: false,
			isWaring: false,
		});
	},
	onConfirm() {
		// console.log("ssid:", this.data.ssid, ",password:", this.data.password)
		if (this.data.isFirst) {
			let udp = wx.createUDPSocket();
			this.setData({
				isFirst: false,
				udp,
				port: udp.bind(),
			});
		}

		const password = this.data.password;
		const ssid = this.data.ssid;
		const port = this.data.port;

		let message = JSON.stringify({
			port,
			password,
			ssid
		})

		this.data.udp.send({
			address: '192.168.4.1',
			port: 1000,
			message
		});


		this.data.udp.onMessage((res) => {
			//字符串转换，很重要
			let unit8Arr = new Uint8Array(res.message);
			let encodedString = String.fromCharCode.apply(null, unit8Arr);
			let data = decodeURIComponent(escape((encodedString)));
			let str = JSON.parse(data);
			switch (str.code) {
				//收到信息
				case 0:
					wx.showToast({
						title: '接收成功',
					})
					break;
				//准备连接路由器
				case 1:
					wx.showToast({
						title: '准备连接路由器',
					})
					break;
				//成功连接到路由器
				case 2:
					wx.showToast({
						title: '成功连接',
					})
					wx.switchTab({
						url: '/pages/index/index',
					})
					break;
				//连接失败路由器
				case 3:
					wx.showToast({
						title: '连接失败',
					})
					break;
			}
		})
	},
	onShow() {
		const prt = wx.getStorageSync('product_type');
		if (prt === 'a11uQJCzUPe') {
			// 西纳舒岩盐气溶胶治疗仪
			this.setData({
				product_type: 'https://gitee.com/tpyszc/tpyszc_image/raw/master/xns.png'
			});
		} else if (prt === 'a1L4GogBzON') {
				this.setData({
					product_type: 'https://gitee.com/tpyszc/tpyszc_image/raw/master/salt.png'
				});
			} else {
				this.setData({
					product_type: 'https://gitee.com/tpyszc/tpyszc_image/raw/master/wf.png'
				});
			}
	}
});